$(document).ready(function() {
	ItemDetails.init();
});

ItemDetails = {};

ItemDetails.init = function() {
	//Contact Seller
	$('#itemDetailsLinkContactSeller').bind('click', function() {
		$("#itemDetailsContatSellerDialog").dialog({
			resizable : false,
			modal : true,
			minWidth: 500,
			draggable : false,
			buttons : [ {
				text : $('#itemDetailsContatSellerDialogOk').text(),
				click : function() {
					var inputSeller = $('#itemDetailsContatSellerDialogInputSeller').val();
					var inputItem = $('#itemDetailsContatSellerDialogInputItem').val();
					var inputMessage = $('#itemDetailsContatSellerDialogInputMessage').val();
					if(inputMessage.trim() != ''){
						var link = $('#itemDetailsLinkContactSeller').attr('link');
						$.post(link, {
							recipient: inputSeller,
							item: inputItem,
							message : inputMessage
						});
						$('#itemDetailsContatSellerDialogInputMessage').val('');
						$(this).dialog("close");
					}
				}
			}, {
				text : $('#itemDetailsContatSellerDialogCancel').text(),
				click : function() {
					$(this).dialog("close");
				}
			} ]
		});
		return false;
	});
	
	//Admin Delete
	$('#itemActionLinkDeleteAdmin').bind('click', function() {
		$("#itemActionDeleteAdminConfirmDialog").dialog({
			resizable : false,
			modal : true,
			draggable: false,
			buttons : [ {
				text : $('#itemActionDeleteAdminConfirmDialogOk').text(),
				click : function() {
					Util.redirect($('#itemActionLinkDeleteAdmin').attr('link'));
				}
			}, {
				text : $('#itemActionDeleteAdminConfirmDialogCancel').text(),
				click : function() {
					$(this).dialog("close");
				}
			} ]
		});
		return false;
	})
	//Seller Delete
	$('#itemActionLinkDeleteSeller').bind('click', function() {
		$("#itemActionDeleteSellerConfirmDialog").dialog({
			resizable : false,
			modal : true,
			draggable: false,
			buttons : [ {
				text : $('#itemActionDeleteSellerConfirmDialogOk').text(),
				click : function() {
					Util.redirect($('#itemActionLinkDeleteSeller').attr('link'));
				}
			}, {
				text : $('#itemActionDeleteSellerConfirmDialogCancel').text(),
				click : function() {
					$(this).dialog("close");
				}
			} ]
		});
		return false;
	})
	//Rating Box
	var ratingBox = $('#itemDetailsGiveFeedbackRatingBox');
	var ratingRuby = new Array();
	ratingRuby[0] = $('#itemDetailsGiveFeedbackRatingBoxRuby1');
	ratingRuby[1] = $('#itemDetailsGiveFeedbackRatingBoxRuby2');
	ratingRuby[2] = $('#itemDetailsGiveFeedbackRatingBoxRuby3');
	ratingRuby[3] = $('#itemDetailsGiveFeedbackRatingBoxRuby4');
	ratingRuby[4] = $('#itemDetailsGiveFeedbackRatingBoxRuby5');
	var ratingInput = $('#itemDetailsGiveFeedbackRatingInput');
	
	var setRubyFromInput = function(){
		var value = ratingInput.val();
		if(Util.isNumber(value) && value <= 5 && value >= 1){
			setRuby(value-1);
		}else{
			setRuby(-1);
		}
	}
	
	var clickRuby = function(v){
		ratingInput.val(v+1);
	}
	var setRuby = function(v){
		for(var i = 0; i < 5;i++){
			if(i <= v){
				ratingRuby[i].css('backgroundImage', 'url(/images/rating/ruby_full.png)');
			}else{
				ratingRuby[i].css('backgroundImage', 'url(/images/rating/ruby_empty.png)')
			}
			
		}
	}
	ratingRuby[0].mouseover(function(e){setRuby(0)});
	ratingRuby[1].mouseover(function(e){setRuby(1)});
	ratingRuby[2].mouseover(function(e){setRuby(2)});
	ratingRuby[3].mouseover(function(e){setRuby(3)});
	ratingRuby[4].mouseover(function(e){setRuby(4)});
	
	ratingRuby[0].click(function(e){clickRuby(0)});
	ratingRuby[1].click(function(e){clickRuby(1)});
	ratingRuby[2].click(function(e){clickRuby(2)});
	ratingRuby[3].click(function(e){clickRuby(3)});
	ratingRuby[4].click(function(e){clickRuby(4)});
	
	ratingBox.mouseout(function(e){
		setRubyFromInput();
	});

	//First Time load
	setRubyFromInput();
	
	//Feedback remove links (admin)
	$('.feedbackRemoveLink').click(function(e){
		var id = $(this).attr('feedbackId')
		$("#itemFeedbackRemoveConfirmDialog").dialog({
			resizable : false,
			modal : true,
			draggable: false,
			buttons : [ {
				text : $('#itemFeedbackRemoveConfirmDialogOk').text(),
				click : function() {
					Util.redirect('/item/action/removeFeedback/'+id);
				}
			}, {
				text : $('#itemFeedbackRemoveConfirmDialogCancel').text(),
				click : function() {
					$(this).dialog("close");
				}
			} ]
		});
		return false;
	});
	
	$('#imageFrameLeft').disableSelection();
	$('#imageFrameRight').disableSelection();
	
	//ImageSwitcher
	if(ItemDetails.maxImage > 0){
		$('#imageFrameLeft').bind('click', function(e){
			if(ItemDetails.currentImage == 0){
				ItemDetails.showImage((ItemDetails.maxImage-1));	
			}else{
				ItemDetails.showImage((ItemDetails.currentImage - 1));	
			}
		});
		$('#imageFrameRight').bind('click', function(e){
			if(ItemDetails.currentImage == (ItemDetails.maxImage-1)){
				ItemDetails.showImage(0);	
			}else{
				ItemDetails.showImage((ItemDetails.currentImage + 1));	
			}
		});
		ItemDetails.showImage(0);		
	}else{
		$('#imageBotText').html('<br>');
	}
	
	//Google Map
	var locationLatitude = $('#locationLatitude').val();
	var locationLongitude = $('#locationLongitude').val();
	if(locationLatitude != '' && locationLongitude != ''){
		var geoCoder = new google.maps.Geocoder();
		geoCoder.geocode({'latLng': new google.maps.LatLng(locationLatitude, locationLongitude)}, 
    		function(results, status) {
  				if(results.length == 0) {
  					console.log('not found');
					return false;
				}
      			if(status == google.maps.GeocoderStatus.OK) {
        			if(results[0]) {
        				$('#itemLocation').html('<b>' + results[0].formatted_address + '</b> <a href="#showMap" onclick="javascript:ItemDetails.showMap();return false;"><img style="vertical-align: middle;padding-bottom:2px;" src="/images/icons/map.png"></a>');
        				ItemDetails.foundAddress = true;
        			}
     			} else {
     				//Error.. just ignore
     			}
		});
	}
}

ItemDetails.showMap = function(){
	var locationLatitude = $('#locationLatitude').val();
	var locationLongitude = $('#locationLongitude').val();
	$('#mapDialog').dialog({
		width : 500,
		height : 400,
		modal : true
	});
	var mapOptions = {
			center: new google.maps.LatLng(locationLatitude, locationLongitude),
			zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP
		};
	map = new google.maps.Map(document.getElementById("dialogMapCanvas"), mapOptions);
	marker = new google.maps.Marker({
		map: map,
		position: map.getCenter()
	});
}

ItemDetails.foundAddress = false;

ItemDetails.currentImage = 0;
ItemDetails.maxImage = 0;

ItemDetails.showImage = function(imageN){
	ItemDetails.hideAllImages();
	ItemDetails.currentImage = imageN;
	$('#imageId-'+imageN).css('display', 'block');
	$('#imageBotTextCurrent').text((imageN+1));
}

ItemDetails.hideAllImages = function(){
	for(var i = 0; i < ItemDetails.maxImage; i++){
		$('#imageId-'+i).css('display', 'none');
	}
}